package com.ruoyi.crm.service;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.domain.CrmStockMedicine;
import com.ruoyi.crm.domain.CrmStockMedicineVisible;
import com.ruoyi.crm.mapper.CrmStockMedicineMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.common.core.domain.entity.SysUser;

/**
 * 药品信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-03
 */
@Service
public class ICrmStockMedicineServiceImpl implements ICrmStockMedicineService
{
    @Autowired
    private CrmStockMedicineMapper crmStockMedicineMapper;
    
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询药品信息
     * 
     * @param medicineId 药品ID
     * @return 药品信息
     */
    @Override
    public CrmStockMedicine selectCrmStockMedicineById(Long medicineId)
    {
        CrmStockMedicine medicine = crmStockMedicineMapper.selectCrmStockMedicineById(medicineId);
        // 查询药品可见用户关联信息
        List<CrmStockMedicineVisible> visibleList = crmStockMedicineMapper.selectCrmStockMedicineVisibleByMedicineId(medicineId);
        if (visibleList != null && !visibleList.isEmpty()) {
            Long[] userIds = visibleList.stream().map(CrmStockMedicineVisible::getUserId).toArray(Long[]::new);
            medicine.setVisibleUserIds(userIds);
            
            // 查询用户名称并设置
            List<SysUser> users = userMapper.selectUserByIds(userIds);
            String userNames = users.stream().map(SysUser::getNickName).collect(Collectors.joining(", "));
            medicine.setVisibleUserNames(userNames);
        }
        return medicine;
    }

    /**
     * 查询药品信息列表
     * 
     * @param crmStockMedicine 药品信息
     * @return 药品信息
     */
    @Override
    public List<CrmStockMedicine> selectCrmStockMedicineList(CrmStockMedicine crmStockMedicine)
    {
        List<CrmStockMedicine> medicines = crmStockMedicineMapper.selectCrmStockMedicineList(crmStockMedicine);
        
        // 为每个药品查询其可见用户关联信息
        for (CrmStockMedicine medicine : medicines) {
            List<CrmStockMedicineVisible> visibleList = crmStockMedicineMapper.selectCrmStockMedicineVisibleByMedicineId(medicine.getMedicineId());
            if (visibleList != null && !visibleList.isEmpty()) {
                Long[] userIds = visibleList.stream().map(CrmStockMedicineVisible::getUserId).toArray(Long[]::new);
                medicine.setVisibleUserIds(userIds);
                
                // 查询用户名称并设置
                List<SysUser> users = userMapper.selectUserByIds(userIds);
                String userNames = users.stream().map(SysUser::getNickName).collect(Collectors.joining(", "));
                medicine.setVisibleUserNames(userNames);
            }
        }
        
        return medicines;
    }

    /**
     * 新增药品信息
     * 
     * @param crmStockMedicine 药品信息
     * @return 结果
     */
    @Override
    public int insertCrmStockMedicine(CrmStockMedicine crmStockMedicine)
    {
        int rows = crmStockMedicineMapper.insertCrmStockMedicine(crmStockMedicine);
        // 如果设置了可见用户，则保存关联关系
        insertCrmStockMedicineVisible(crmStockMedicine);
        return rows;
    }

    /**
     * 修改药品信息
     * 
     * @param crmStockMedicine 药品信息
     * @return 结果
     */
    @Override
    public int updateCrmStockMedicine(CrmStockMedicine crmStockMedicine)
    {
        // 先删除原有的可见用户关联
        crmStockMedicineMapper.deleteCrmStockMedicineVisibleByMedicineIds(new Long[]{crmStockMedicine.getMedicineId()});
        // 更新药品信息
        int rows = crmStockMedicineMapper.updateCrmStockMedicine(crmStockMedicine);
        // 重新插入可见用户关联
        insertCrmStockMedicineVisible(crmStockMedicine);
        return rows;
    }

    /**
     * 批量删除药品信息
     * 
     * @param medicineIds 需要删除的药品ID
     * @return 结果
     */
    @Override
    public int deleteCrmStockMedicineByIds(Long[] medicineIds)
    {
        // 删除药品可见用户关联
        crmStockMedicineMapper.deleteCrmStockMedicineVisibleByMedicineIds(medicineIds);
        // 删除药品信息
        return crmStockMedicineMapper.deleteCrmStockMedicineByIds(medicineIds);
    }
    
    /**
     * 新增药品可见用户关联信息
     * 
     * @param crmStockMedicine 药品信息对象
     */
    public void insertCrmStockMedicineVisible(CrmStockMedicine crmStockMedicine)
    {
        Long[] visibleUserIds = crmStockMedicine.getVisibleUserIds();
        if (visibleUserIds != null && visibleUserIds.length > 0)
        {
            List<CrmStockMedicineVisible> list = new java.util.ArrayList<CrmStockMedicineVisible>();
            for (Long userId : visibleUserIds)
            {
                CrmStockMedicineVisible visible = new CrmStockMedicineVisible();
                visible.setMedicineId(crmStockMedicine.getMedicineId());
                visible.setUserId(userId);
                visible.setCreateBy(crmStockMedicine.getCreateBy());
                visible.setCreateTime(crmStockMedicine.getCreateTime());
                list.add(visible);
            }
            if (list.size() > 0)
            {
                crmStockMedicineMapper.batchCrmStockMedicineVisible(list);
            }
        }
    }
}